home *** CD-ROM | disk | FTP | other *** search
Wrap
# Source Generated with Decompyle++ # File: in.pyc (Python 2.5) import os import random import re import sys import sha import time import string import urllib import socket import logging import filetypes import tempfile import threading import traceback import subprocess from clock import clock from types import UnicodeType, StringType from BitTorrent.bencode import bdecode, bencode chatter = True inDownloader = False ignoreErrors = False def quoteJS(x): x = x.replace('\\', '\\\\') x = x.replace('"', '\\"') x = x.replace("'", "\\'") x = x.replace('\n', '\\n') x = x.replace('\r', '\\r') return x def getNiceStack(): """Get a stack trace that's a easier to read that the full one. """ stack = traceback.extract_stack() while (len(stack) > 0 or os.path.basename(stack[0][0]) == 'unittest.py' or isinstance(stack[0][3], str)) and stack[0][3].startswith('unittest.main'): stack = stack[1:] for i in xrange(len(stack)): if os.path.basename(stack[i][0]) == 'util.py' and stack[i][2] in ('failed', 'failedExn'): stack = stack[:i + 1] break continue stack = _[1] return stack def readSimpleConfigFile(path): ret = { } f = open(path, 'rt') for line in f.readlines(): if re.match('^[ \t]*$', line): continue match = re.match('^([^ ]+) *= *([^\\r\\n]*)[\\r\\n]*$', line) if not match: print "WARNING: %s: ignored bad configuration directive '%s'" % (path, line) continue key = match.group(1) value = match.group(2) if key in ret: print "WARNING: %s: ignored duplicate directive '%s'" % (path, line) continue ret[key] = value return ret def writeSimpleConfigFile(path, data): f = open(path, 'wt') for k, v in data.iteritems(): f.write('%s = %s\n' % (k, v)) f.close() def queryRevision(f): try: p = subprocess.Popen([ 'svn', 'info', f], stdout = subprocess.PIPE) info = p.stdout.read() p.stdout.close() url = re.search('URL: (.*)', info).group(1) url = url.strip() revision = re.search('Revision: (.*)', info).group(1) revision = revision.strip() return (url, revision) except KeyboardInterrupt: raise except: return None def absolutePathToFileURL(path): if isinstance(path, unicode): path = path.encode('utf-8') parts = string.split(path, os.sep) parts = [ urllib.quote(x, ':') for x in parts ] return 'file://' + '/'.join(parts) def failedExn(when, **kwargs): failed(when, withExn = True, **kwargs) def failed(when, withExn = False, details = None): logging.info('failed() called; generating crash report.') header = '' try: import config import prefs header += 'App: %s\n' % config.get(prefs.LONG_APP_NAME) header += 'Publisher: %s\n' % config.get(prefs.PUBLISHER) header += 'Platform: %s\n' % config.get(prefs.APP_PLATFORM) header += 'Python: %s\n' % sys.version.replace('\r\n', ' ').replace('\n', ' ').replace('\r', ' ') header += 'Py Path: %s\n' % repr(sys.path) header += 'Version: %s\n' % config.get(prefs.APP_VERSION) header += 'Serial: %s\n' % config.get(prefs.APP_SERIAL) header += 'Revision: %s\n' % config.get(prefs.APP_REVISION) header += 'Builder: %s\n' % config.get(prefs.BUILD_MACHINE) header += 'Build Time: %s\n' % config.get(prefs.BUILD_TIME) except KeyboardInterrupt: raise except: pass header += 'Time: %s\n' % time.asctime() header += 'When: %s\n' % when header += '\n' if withExn: header += 'Exception\n---------\n' header += ''.join(traceback.format_exception(*sys.exc_info())) header += '\n' if details: header += 'Details: %s\n' % (details,) header += 'Call stack\n----------\n' try: stack = getNiceStack() except KeyboardInterrupt: raise except: stack = traceback.extract_stack() header += ''.join(traceback.format_list(stack)) header += '\n' header += 'Threads\n-------\n' header += 'Current: %s\n' % threading.currentThread().getName() header += 'Active:\n' for t in threading.enumerate(): if not t.isDaemon() or ' [Daemon]': pass header += ' - %s%s\n' % (t.getName(), '') report = '{{{\n%s}}}\n' % header def readLog(logFile, logName = 'Log'): try: f = open(logFile, 'rt') logContents = '%s\n---\n' % logName logContents += f.read() f.close() except KeyboardInterrupt: raise except: logContents = '' return logContents logFile = config.get(prefs.LOG_PATHNAME) downloaderLogFile = config.get(prefs.DOWNLOADER_LOG_PATHNAME) if logFile is None: logContents = 'No logfile available on this platform.\n' else: logContents = readLog(logFile) if downloaderLogFile is not None: if logContents is not None: logContents += '\n' + readLog(downloaderLogFile, 'Downloader Log') else: logContents = readLog(downloaderLogFile) if logContents is not None: report += '{{{\n%s}}}\n' % stringify(logContents) logging.info('----- CRASH REPORT (DANGER CAN HAPPEN) -----') logging.info(header) logging.info('----- END OF CRASH REPORT -----') if not inDownloader: try: import dialogs _ = gettext import gtcache if not ignoreErrors: chkboxdialog = dialogs.CheckboxTextboxDialog(_('Internal Error'), _('Miro has encountered an internal error. You can help us track down this problem and fix it by submitting an error report.'), _('Include entire program database including all video and channel metadata with crash report'), False, _('Describe what you were doing that caused this error'), dialogs.BUTTON_SUBMIT_REPORT, dialogs.BUTTON_IGNORE) (chkboxdialog.run,)((lambda x: _sendReport(report, x))) except Exception: e = None logging.exception('Execption when reporting errror..') except: None<EXCEPTION MATCH>Exception None<EXCEPTION MATCH>Exception command = command daemon = daemon import dl_daemon c = command.DownloaderErrorCommand(daemon.lastDaemon, report) c.send() def _sendReport(report, dialog): global ignoreErrors def callback(result): app.controller.sendingCrashReport -= 1 if result['status'] != 200 or result['body'] != 'OK': logging.warning(u'Failed to submit crash report. Server returned %r' % result) else: logging.info(u'Crash report submitted successfully') def errback(error): app.controller.sendingCrashReport -= 1 logging.warning(u'Failed to submit crash report %r' % error) import dialogs import httpclient import config import prefs import app if dialog.choice == dialogs.BUTTON_IGNORE: ignoreErrors = True return None backupfile = None if hasattr(dialog, 'checkbox_value') and dialog.checkbox_value: try: logging.info('Sending entire database') import database backupfile = database.defaultDatabase.liveStorage.backupDatabase() traceback.print_exc() logging.warning(u'Failed to backup database') description = u'Description text not implemented' if hasattr(dialog, 'textbox_value'): description = dialog.textbox_value description = description.encode('utf-8') postVars = { 'description': description, 'app_name': config.get(prefs.LONG_APP_NAME), 'log': report } if backupfile: postFiles = { 'databasebackup': { 'filename': 'databasebackup.zip', 'mimetype': 'application/octet-stream', 'handle': open(backupfile, 'rb') } } else: postFiles = None app.controller.sendingCrashReport += 1 httpclient.grabURL('http://participatoryculture.org/bogondeflector/index.php', callback, errback, method = 'POST', postVariables = postVars, postFiles = postFiles) class AutoflushingStream: '''Converts a stream to an auto-flushing one. It behaves in exactly the same way, except all write() calls are automatically followed by a flush(). ''' def __init__(self, stream): self.__dict__['stream'] = stream def write(self, data): if isinstance(data, unicode): data = data.encode('ascii', 'backslashreplace') self.stream.write(data) self.stream.flush() def __getattr__(self, name): return getattr(self.stream, name) def __setattr__(self, name, value): return setattr(self.stream, name, value) def makeDummySocketPair(): '''Create a pair of sockets connected to each other on the local interface. Used to implement SocketHandler.wakeup(). ''' dummy_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) dummy_server.bind(('127.0.0.1', 0)) dummy_server.listen(1) server_address = dummy_server.getsockname() first = socket.socket(socket.AF_INET, socket.SOCK_STREAM) first.connect(server_address) (second, address) = dummy_server.accept() dummy_server.close() return (first, second) def trapCall(when, function, *args, **kwargs): '''Make a call to a function, but trap any exceptions and do a failedExn call for them. Return True if the function successfully completed, False if it threw an exception ''' try: function(*args, **kwargs) return True except KeyboardInterrupt: raise except: failedExn(when) return False TRACK_CUMULATIVE = False cumulative = { } cancel = False def timeTrapCall(when, function, *args, **kwargs): global cancel, cancel cancel = False start = clock() retval = trapCall(when, function, *args, **kwargs) end = clock() if cancel: return retval if end - start > 1: logging.timing('WARNING: %s too slow (%.3f secs)', when, end - start) if TRACK_CUMULATIVE: try: total = cumulative[when] except KeyboardInterrupt: raise except: total = 0 total += end - start cumulative[when] = total return retval if total > 5: logging.timing('%s cumulative is too slow (%.3f secs)', when, total) cumulative[when] = 0 cancel = True return retval def getTorrentInfoHash(path): f = open(path, 'rb') try: data = f.read() metainfo = bdecode(data) infohash = sha.sha(bencode(metainfo['info'])).digest() return infohash finally: f.close() class ExponentialBackoffTracker: '''Utility class to track exponential backoffs.''' def __init__(self, baseDelay): self.baseDelay = self.currentDelay = baseDelay def nextDelay(self): rv = self.currentDelay self.currentDelay *= 2 return rv def reset(self): self.currentDelay = self.baseDelay def gatherVideos(path, progressCallback): import item import prefs import config import platformutils keepGoing = True parsed = 0 found = list() try: for root, dirs, files in os.walk(path): for f in files: parsed = parsed + 1 if filetypes.isVideoFilename(f): found.append(os.path.join(root, f)) if parsed > 1000: adjustedParsed = int(parsed / 100) * 100 elif parsed > 100: adjustedParsed = int(parsed / 10) * 10 else: adjustedParsed = parsed keepGoing = progressCallback(adjustedParsed, len(found)) if not keepGoing: found = None raise continue if config.get(prefs.SHORT_APP_NAME) in dirs: dirs.remove(config.get(prefs.SHORT_APP_NAME)) continue except KeyboardInterrupt: raise except: pass return found def formatSizeForUser(bytes, zeroString = '', withDecimals = True, kbOnly = False): '''Format an int containing the number of bytes into a string suitable for printing out to the user. zeroString is the string to use if bytes == 0. ''' _ = gettext import gtcache if bytes > 1073741824 and not kbOnly: value = bytes / 1.07374e+09 if withDecimals: format = _('%1.1fGB') else: format = _('%dGB') elif bytes > 1048576 and not kbOnly: value = bytes / 1.04858e+06 if withDecimals: format = _('%1.1fMB') else: format = _('%dMB') elif bytes > 1024: value = bytes / 1024 if withDecimals: format = _('%1.1fKB') else: format = _('%dKB') elif bytes > 1: value = bytes if withDecimals: format = _('%1.1fB') else: format = _('%dB') else: return zeroString return format % value def formatTimeForUser(seconds, sign = 1): """Format a duration in seconds into a string suitable for display, using the minimum amount of digits. Negative durations used for remaining times display a '-' sign. """ (_, _, _, h, m, s, _, _, _) = time.gmtime(seconds) if sign < 0: sign = '-' else: sign = '' if int(seconds) in range(0, 3600): return '%s%d:%02u' % (sign, m, s) else: return '%s%d:%02u:%02u' % (sign, h, m, s) def makeAnchor(label, href): return '<a href="%s">%s</a>' % (href, label) def makeEventURL(label, eventURL): return '<a href="#" onclick="return eventURL(\'action:%s\');">%s</a>' % (eventURL, label) def clampText(text, maxLength): if len(text) > maxLength: return text[:maxLength - 3] + '...' else: return text def print_mem_usage(message): pass class TooManySingletonsError(Exception): pass def getSingletonDDBObject(view): view.confirmDBThread() viewLength = view.len() if viewLength == 1: view.resetCursor() return view.next() elif viewLength == 0: raise LookupError("Can't find singleton in %s" % repr(view)) else: msg = '%d objects in %s' % (viewLength, len(view)) raise TooManySingletonsError(msg) class ThreadSafeCounter: '''Implements a counter that can be access by multiple threads.''' def __init__(self, initialValue = 0): self.value = initialValue self.lock = threading.Lock() def inc(self): self.lock.acquire() try: self.value += 1 finally: self.lock.release() def dec(self): self.lock.acquire() try: self.value -= 1 finally: self.lock.release() def getvalue(self): self.lock.acquire() try: return self.value finally: self.lock.release() def setupLogging(): logging.addLevelName(25, 'TIMING') logging.timing = lambda msg, *args, **kargs: logging.log(25, msg, *args, **kargs) logging.addLevelName(26, 'JSALERT') logging.jsalert = lambda msg, *args, **kargs: logging.log(26, msg, *args, **kargs) class DemocracyUnicodeError(StandardError): pass def checkU(text): if text is not None and type(text) != UnicodeType: raise DemocracyUnicodeError, u'text "%s" is not a unicode string' % text def returnsUnicode(func): def checkFunc(*args, **kwargs): result = func(*args, **kwargs) if result is not None: checkU(result) return result return checkFunc def checkB(text): if text is not None and type(text) != StringType: raise DemocracyUnicodeError, u'text "%s" is not a binary string' % text def returnsBinary(func): def checkFunc(*args, **kwargs): result = func(*args, **kwargs) if result is not None: checkB(result) return result return checkFunc def checkURL(text): if type(text) != UnicodeType: raise DemocracyUnicodeError, u'url "%s" is not unicode' % text try: text.encode('ascii') except: raise DemocracyUnicodeError, u'url "%s" contains extended characters' % text def returnsURL(func): def checkFunc(*args, **kwargs): result = func(*args, **kwargs) if result is not None: checkURL(result) return result return checkFunc def checkF(text): FilenameType = FilenameType import platformutils if text is not None and type(text) != FilenameType: raise DemocracyUnicodeError, u'text "%s" is not a valid filename type' % text def returnsFilename(func): def checkFunc(*args, **kwargs): result = func(*args, **kwargs) if result is not None: checkF(result) return result return checkFunc def unicodify(d): '''Turns all strings in data structure to unicode. ''' if isinstance(d, dict): for key in d.keys(): d[key] = unicodify(d[key]) elif isinstance(d, list): for key in range(len(d)): d[key] = unicodify(d[key]) elif type(d) == StringType: d = d.decode('ascii', 'replace') return d def stringify(u, handleerror = 'xmlcharrefreplace'): '''Takes a possibly unicode string and converts it to a string string. This is required for some logging especially where the things being logged are filenames which can be Unicode in the Windows platform. Note that this is not the inverse of unicodify. You can pass in a handleerror argument which defaults to "xmlcharrefreplace". This will increase the string size as it converts unicode characters that don\'t have ascii equivalents into escape sequences. If you don\'t want to increase the string length, use "replace" which will use ? for unicode characters that don\'t have ascii equivalents. ''' if isinstance(u, unicode): return u.encode('ascii', handleerror) if not isinstance(u, str): return str(u) return u def quoteUnicodeURL(url): '''Quote international characters contained in a URL according to w3c, see: <http://www.w3.org/International/O-URL-code.html> ''' checkU(url) quotedChars = list() for c in url.encode('utf8'): if ord(c) > 127: quotedChars.append(urllib.quote(c)) continue quotedChars.append(c) return u''.join(quotedChars) def no_console_startupinfo(): """Returns the startupinfo argument for subprocess.Popen so that we don't open a console window. On platforms other than windows, this is just None. On windows, it's some win32 sillyness. """ if subprocess.mswindows: startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW return startupinfo else: return None def call_command(*args, **kwargs): """Call an external command. If the command doesn't exit with status 0, or if it outputs to stderr, an exception will be raised. Returns stdout. """ ignore_stderr = kwargs.pop('ignore_stderr', False) if kwargs: raise TypeError('extra keyword arguments: %s' % kwargs) pipe = subprocess.Popen(args, stdout = subprocess.PIPE, stdin = subprocess.PIPE, stderr = subprocess.PIPE, startupinfo = no_console_startupinfo()) (stdout, stderr) = pipe.communicate() if pipe.returncode != 0: raise OSError('call_command with %s has return code %s\nstdout:%s\nstderr:%s' % (args, pipe.returncode, stdout, stderr)) elif stderr and not ignore_stderr: raise OSError('call_command with %s outputed error text:\n%s' % (args, stderr)) else: return stdout def getsize(path): """Get the size of a path. If it's a file, return the size of the file. If it's a directory return the total size of all the files it contains. """ if os.path.isdir(path): size = 0 for dirpath, dirnames, filenames in os.walk(path): for name in filenames: size += os.path.getsize(os.path.join(dirpath, name)) size += os.path.getsize(dirpath) return size else: return os.path.getsize(path) def partition(list, size): '''Partiction list into smaller lists such that none is larger than size elements. Returns a list of lists. The lists appended together will be the original list. ''' retval = [] for start in range(0, len(list), size): retval.append(list[start:start + size]) return retval def directoryWritable(directory): '''Check if we can write to a directory.''' try: f = tempfile.TemporaryFile(dir = directory) except OSError: return False f.close() return True def random_string(length): return ''.join((lambda .0: for i in .0: random.choice(string.ascii_letters))(xrange(length)))